package com.dragon.client.controller.contract;

import ch.qos.logback.core.joran.event.SaxEventRecorder;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.common.model.ResponseData;
import com.dragon.logical.service.contract.IRepairService;
import com.dragon.repository.domain.dto.RepairDTO;
import com.dragon.repository.domain.dto.RepairProductDTO;
import com.dragon.repository.domain.vo.RepairProductVO;
import com.dragon.repository.domain.vo.RepairVO;
import com.dragon.repository.entity.Repair;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 维修单据表 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-12-05
 */
@RestController
@RequestMapping("repair")
@Api(tags = "维修管理")
public class RepairController {

    @Autowired
    IRepairService service;



    @GetMapping("list")
    @ApiOperation(value = "获取维修列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",required = false,type = "string",value = "合同名称"),
            @ApiImplicitParam(name="status",required = false,type = "int",value = "维修状态"),
            @ApiImplicitParam(name="page",required = true,type = "int",value = "页码"),
            @ApiImplicitParam(name="limit",required = true,type = "int",value = "条数")
    })
    public ResponseData selectListPage(@RequestParam(required = false) String name,@RequestParam(required = false) Integer status,
                                       @RequestParam Integer page,@RequestParam Integer limit){
        Page<Repair> repairPage = service.selectListPage(name, status, page, limit);
        return ResponseData.success(RepairDTO.convertList(repairPage.getRecords()),repairPage.getTotal());
    }


    @GetMapping("detail")
    @ApiOperation(value = "获取维修详情")
    public ResponseData selectDetailById(@RequestParam @ApiParam(name = "id",required = true,type = "int",value = "主id") Integer id){
        Repair repair = service.selectDetailById(id);
        RepairDTO repairDTO = RepairDTO.convert(repair);
        repairDTO.setRepairProductDTOS(RepairProductDTO.convertList(repair.getRepairProducts()));
        return ResponseData.success(repairDTO);
    }


    @PostMapping("add")
    @ApiOperation(value = "添加维修单")
    public ResponseData addRepair(@RequestBody RepairVO vo){
        Repair repair = RepairVO.convert(vo);
        repair.setRepairProducts(RepairProductVO.convertList(vo.getRepairProducts()));
        service.addRepair(repair);
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除维修单")
    public ResponseData deleteRepair(@RequestParam @ApiParam(name = "id",required = true,type = "int",value = "主id") Integer id){
        service.delete(id);
        return ResponseData.success();
    }

    @PostMapping("updateStatus")
    @ApiOperation(value = "修改状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",required = true,type = "int",value = "主id"),
            @ApiImplicitParam(name = "status",required = true,type = "int",value = "状态:0、维修中，1、维修完成")
    })
    public ResponseData updateStatus(@RequestParam Integer id,@RequestParam Integer status){
        service.updateStatus(id,status);
        return ResponseData.success();
    }



}
